
//
//  MessageHomeViewController.m
//  HuLaQuan
//
//  Created by hok on 1/15/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "MessageHomeViewController.h"

#import "MessageListCell.h"

#import "MessageDetailInfoViewController.h"

#import "HomeTabBarController.h"

//logic model
#import "SearchMessageEngine.h"

#import "JHRefresh.h"

@interface MessageHomeViewController  ()

@property (strong, nonatomic) NSMutableArray *messageList; //of keys;

@property (strong, nonatomic) NSIndexPath *selectIndexPath;

@property(nonatomic,strong) SearchMessageEngine *searchMessageEngine;

@end

static NSString * const MessageListCellIdentifier = @"MessageListCell";

@implementation MessageHomeViewController

-(SearchMessageEngine *)searchMessageEngine
{
    if (!_searchMessageEngine) {
        _searchMessageEngine = [[SearchMessageEngine alloc] init];
    }
    return _searchMessageEngine;
}
- (NSMutableArray *)messageList
{
    if (!_messageList) {
        _messageList = [[NSMutableArray alloc] init];
    }
    return _messageList;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [(HomeTabBarController *)self.tabBarController setTabBarHidden:NO];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SearchMessageFinish:) name:@"SearchMessageFinish" object:nil];
    
    [self.searchMessageEngine searchMessage:@""];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"SearchMessageFinish" object:nil];
    
}
/*
 上拉加载，下拉刷新
 */
- (void)addRefreshView
{
    __weak typeof (self) wealSelf = self;
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
//        _start = 1;
        [wealSelf initData];
        [wealSelf.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }];
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
//        wealSelf.start++;
        [wealSelf initData];
        [wealSelf.tableView footerEndRefreshing];
    }];
    
}
#pragma mark - controller lifecycle
- (void)SearchMessageFinish:(NSNotification*) notification
{
    //
    //   dispatch_async(dispatch_get_main_queue(), ^{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    //  });
    
}
- (void)setupUI
{
    
    [self setupFooterForTableView];
    
    
    [self.tableView registerNib:[MessageListCell nib] forCellReuseIdentifier:MessageListCellIdentifier];
    
    self.title = @"消息";
    
    
    [self addRefreshView];
}


#pragma mark UITableViewDataSource
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return UITableViewCellEditingStyleDelete;
    
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
        
        [self.messageList removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kMessageListCellHeight;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messageList.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return kUserCenterSectionHeaderHeight;
    
    
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //设置删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        [self.messageList removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }];
    
    
    
    //    collectRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
      deleteRowAction.backgroundColor = [UIColor orangeColor];
    // collectRowAction.backgroundColor = [UIColor grayColor];
    
    return  @[deleteRowAction];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    MessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:MessageListCellIdentifier
                                                             forIndexPath:indexPath];
    // cell.noticeLabel.text = @"请添加新账户";
    
   // cell.titleLabel.text = self.messageList[indexPath.row][@"title"];
    cell.titleLabel.text = self.messageList[indexPath.row];
    return cell;
    
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self performSegueWithIdentifier:@"ShowMessageDetailInfoViewController" sender:self];
}

 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
     
     [(HomeTabBarController *)self.tabBarController setTabBarHidden:YES];
     
     if ([[segue identifier] isEqualToString:@"ShowMessageDetailInfoViewController"]) {
         MessageDetailInfoViewController *controller = (MessageDetailInfoViewController *)[segue destinationViewController];
//         controller.MessageTitle=
     }
 }

@end
